//
//  PandaCollectionFetcher.swift
//  demo3
//
//  Created by raymond on 2022/6/5.
//

import Foundation

@MainActor
class PandaCollectionFetcher: ObservableObject {
  @Published var imageData = PandaCollection(sample: [Panda.defaultPanda])
  @Published var currentPanda = Panda.defaultPanda
  
  let urlString = "http://playgrounds-cdn.apple.com/assets/pandaData.json"
  
  enum FetchError: Error {
    case badRequest
    case badJSON
  }
  
  @available(iOS 15.0, *)
  func fetchData() async throws {
    guard let url = URL(string: urlString) else { return }
    
    let (data, response) =  try await URLSession.shared.data(for: URLRequest(url: url))
    guard (response as? HTTPURLResponse)?.statusCode == 200 else { throw FetchError.badRequest }

    imageData = try JSONDecoder().decode(PandaCollection.self, from: data)
  }
}
